package com.swxc.smartorder.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.model.HttpParams
import com.swxc.smartorder.R
import com.swxc.smartorder.adapter.AdapterWork
import com.swxc.smartorder.app.GlobalAPI
import com.swxc.smartorder.entity.CommonData
import com.swxc.smartorder.entity.CommonListData
import com.swxc.smartorder.entity.OrderBean
import com.swxc.smartorder.network.NetworkHelper
import com.swxc.smartorder.ui.activity.OrderDetailActivity
import com.swxc.smartorder.ui.base.BaseFragment
import com.swxc.smartorder.utils.JsonUtil
import kotlinx.android.synthetic.main.fragment_order_fragmetn.*
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager
import java.util.*


private const val ARG_PARAM1 = "param1"


class OrderFragment : BaseFragment() {

    companion object {

        @JvmStatic
        fun newInstance(param1: String) =
                OrderFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }
    }

    override fun getContentViewLayoutID(): Int {
        return R.layout.fragment_order_fragmetn
    }

    override fun initViewsAndEvents(view: View?) {
        mList = ArrayList()
        mAdapter = AdapterWork(mList)
        mHelper = NetworkHelper(context, activity?.supportFragmentManager!!)
        rv_order.layoutManager = LinearLayoutManager(activity)
        rv_order.adapter = mAdapter
        mAdapter?.setOnItemClickListener { adapter, _, position ->
            val item: OrderBean = adapter.data[position] as OrderBean
            val bundle = Bundle()
            bundle.putLong("orderID", item.id)
            readyGo(OrderDetailActivity::class.java, bundle)
        }

        val emptyView = layoutInflater.inflate(R.layout.layout_empty, null, false)

        val tvEmpty = emptyView.findViewById<TextView>(R.id.tv_empty)
        emptyView.findViewById<TextView>(R.id.tv_refresh).setOnClickListener { initData() }
        var string: String? = null
        when (choseType) {
            "all" -> string = "没有工单"
            "accepted" -> string = "没有受理工单"
            "commited" -> string = "没有我提交的工单"
            "untreated" -> string = "没有待接工单"
        }
        tvEmpty.text = string

        statusLayoutManager = StatusLayoutManager.Builder(refreshLayout)
                .setEmptyLayout(emptyView)
                .setErrorLayout(R.layout.layout_error)
                .setDefaultLayoutsBackgroundColor(resources.getColor(R.color.ui_gray))
                .setOnStatusChildClickListener(object : OnStatusChildClickListener {
                    override fun onErrorChildClick(p0: View?) {
                        initData()
                    }

                    override fun onEmptyChildClick(p0: View?) {
                    }

                    override fun onCustomerChildClick(p0: View?) {
                    }
                })
                .build()

        refreshLayout.setOnRefreshListener {

            initData()
        }
    }

    override fun onFirstUserVisible() {
        initData()
    }

    override fun onUserVisible() {
    }

    override fun onUserInvisible() {

    }

    override fun destroyViewAndThing() {
    }

    private var statusLayoutManager: StatusLayoutManager? = null
    private var choseType: String? = null
    private var mHelper: NetworkHelper? = null
    private var mList: MutableList<OrderBean>? = null
    private var mAdapter: AdapterWork? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            choseType = it.getString(ARG_PARAM1)
        }

    }


    private fun initData() {

        val httpParams = HttpParams()
        httpParams.put("chose", choseType)
        httpParams.put("fields", "id,status,title,createTime,priorityLevel,timeout")
        httpParams.put("page.no", 1)
        httpParams.put("page.size", 50)
        httpParams.put("searchKeyWord", "")
        mHelper?.getRequest(GlobalAPI.orderList, httpParams, object : NetworkHelper.NetWorkHelperBack {
            override fun requestSuccess(json: String) {

                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                val type = object : TypeToken<CommonData<CommonListData<OrderBean>>>() {}.type
                val fromJson = JsonUtil.fromJson<CommonData<CommonListData<OrderBean>>>(json, type)
                        ?: return
                if (fromJson.success) {
                    if (fromJson.data.elements.isEmpty()) {
                        statusLayoutManager?.showEmptyLayout()
                    } else {
                        statusLayoutManager?.showSuccessLayout()
                        mList?.clear()
                        mList?.addAll(fromJson.data.elements)
                        mAdapter?.notifyDataSetChanged()
                    }

                }
            }

            override fun requestFail() {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                statusLayoutManager?.showErrorLayout()
            }
        })


    }


}
